package br.com.trabalho.daohibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.com.trabalho.hibernate.Cliente;
import br.com.trabalho.hibernate.util.HibernateUtil;
import br.com.trabalho.dao.ClienteDAO;

public class ClienteDaoHibernate implements ClienteDAO
{
	private Session session;
	

	public void alterar(Cliente c) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.update(c);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public Cliente consultar(Cliente cliente) {
		Cliente retorno = new Cliente();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			retorno = (Cliente) session.get(Cliente.class, cliente.getNome());
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return retorno;
	}

	public void excluir(Cliente c) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.delete(c);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	public boolean existe(Cliente cliente) {

		Cliente c = consultar(cliente);
		return (c.getNome() != null);
	}

	public void inserir(Cliente c) {
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			session.save(c);
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
	}

	
	@SuppressWarnings("unchecked")
	public List<Cliente> listar() {
		List<Cliente> lista = new ArrayList<Cliente>();
		try {
			session = HibernateUtil.getSession();
			session.beginTransaction();
			lista = (List<Cliente>) session.createCriteria(Cliente.class).list();
			session.getTransaction().commit();
		} catch (Exception e) {
			session.getTransaction().rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return lista;
	}

	@SuppressWarnings("unchecked")
	public List<Cliente> listar(Cliente cliente) {
		Criteria c = session.createCriteria(Cliente.class);
		if (cliente.getNome().length() > 0) {
			c.add(Restrictions.like("nome", cliente.getNome() + "%"));
		}
		c.addOrder(Order.asc("nome"));
		return (List<Cliente>) c.list();
	}

	@Override
	public Cliente getById(int codigo) {
			Cliente cliente = new Cliente();
			try {
				session = HibernateUtil.getSession();
				session.beginTransaction();
				cliente = (Cliente) session.get(Cliente.class, codigo);
				session.getTransaction().commit();
			} catch (Exception e) {
				session.getTransaction().rollback();
				e.printStackTrace();
			} finally {
				session.close();
			}
			return cliente;
		}
}
